﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZY.EntityFrameWork.Core.Model.Entity;
using ZY.EntityFrameWork.Core.Model.Entity.Authority;

namespace ZY.EntityFrameWork.Core.Context
{
    //初始化器
    //DropCreateDatabaseIfModelChanges
    //DropCreateDatabaseAlways
    public class SeedingDataInitializer : DropCreateDatabaseIfModelChanges<HZKContext>
    {
        protected override void Seed(HZKContext context)
        {
            for (int i = 0; i < 6; i++)
            {
                ArchiveInfo info=new ArchiveInfo{ArvID=Convert.ToString(1000+i), ArvStatus="在库"};
                context.Set<ArchiveInfo>().Add(info);
            }


            ////权限
            //var permissions = new List<Permission> 
            //{
            //    new Permission {Code = "Add", Name = "新增", Enabled = true},
            //    new Permission {Code = "Edit", Name = "编辑", Enabled = true},
            //    new Permission {Code = "Delete", Name = "删除", Enabled = true},
            //    new Permission {Code = "Control", Name = "control", Enabled = true},
            //};
            //context.Set<Permission>().AddRange(permissions);

            //模块
            context.Set<Module>().AddRange(new Module[]
            {
                 new Module
                 {
                        Id=0,
                        ModuleTag="100",
                        ModuleName="档案操作",
                        Enabled=true,
                        ChildModule=new List<Module>
                        {
                            new Module{Id=1,ModuleTag="101",ModuleName="档案入库",Enabled=true,Permissions=15},
                            new Module{Id=2,ModuleTag="102",ModuleName="档案出库",Enabled=true,Permissions=15},
                            new Module{Id=3,ModuleTag="103",ModuleName="档案移库",Enabled=false,Permissions=15},
                            new Module{Id=4,ModuleTag="104",ModuleName="档案盘库",Enabled=false,Permissions=15},
                        }
                   },    
                 new Module
                 {
                        Id=5,
                        ModuleTag="200",
                        ModuleName="档案操作",
                        Enabled=true,
                        ChildModule=new List<Module>
                        {
                            new Module{Id=6,ModuleTag="201",ModuleName="档案借阅",Enabled=true},
                            new Module{Id=7,ModuleTag="202",ModuleName="档案归还",Enabled=true},
                            new Module{Id=8,ModuleTag="203",ModuleName="档案查询",Enabled=false},
                            new Module{Id=9,ModuleTag="204",ModuleName="档案统计",Enabled=false},
                        }
                 }
            });

            //List<RoleModule> modulePermissions = new List<RoleModule>();
            //foreach (Module module in modules)
            //{
            //    foreach (Permission op in permissions)
            //    {
            //        if (module.Enabled)
            //        {
            //            modulePermissions.Add(new RoleModule { Module = module, Permission = op, RoleName = "管理员" });                    
            //        }
            //        //context.Set<ModulePermission>().Add(new ModulePermission { Module = module, Permission = op, RoleName = "管理员" });
            //    }
            //}

            context.Set<Role>().AddRange(new Role[]
            {
                new Role{Id = 0,RoleName = "管理员" },
                new Role{Id = 1,RoleName = "普通用户" },
            });

            context.Set<RoleModule>().AddRange(new RoleModule[]
            {
                //管理员权限
                new RoleModule{RoleId=0,ModuleId=1,Permissions=15 },
                new RoleModule{RoleId=0,ModuleId=2,Permissions=15 },
                new RoleModule{RoleId=0,ModuleId=3,Permissions=15 },
                new RoleModule{RoleId=0,ModuleId=4,Permissions=15 },

                //普通用户权限
                new RoleModule{RoleId=1,ModuleId=1,Permissions=15 },
                new RoleModule{RoleId=1,ModuleId=2,Permissions=15 },
                new RoleModule{RoleId=1,ModuleId=3,Permissions=15 },
                new RoleModule{RoleId=1,ModuleId=4,Permissions=15 },
            });


            context.Set<User>().Add(new User
            {
                UserCode = "abc",
                Password = "123456",
                UserName = "一个",
                //RoleName = "管理员"
                RoleId = 0
            });

            base.Seed(context);
        }
    }
}
